/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Thumbnail.java
 *
 * Created on 27 sept. 2009, 15:57:34
 */

package jvgallery;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.border.EmptyBorder;

public class Thumbnail extends javax.swing.JPanel {

    public String imagePath;
    private BufferedImage image;
    int x,y;

    /**
     * Constructor
     * @param image
     */
    public Thumbnail(String imgpath, float scaleRatio)
    {
        super();
        this.imagePath = imgpath;

        // Image position inside container
        this.x = 0;
        this.y = 0;

        // Load image
        BufferedImage image = this.loadImage(imgpath);
        image = this.scaleImage(image, 0.2);
        this.image = image;

        // Netbeans
        initComponents();

        // Border settings
        this.setBorder(new EmptyBorder(0,0,0,0));
    }

    /**
     * Load an image from its path on the filesystem
     * @param imgpath
     * @return
     */
    private BufferedImage loadImage(String imgpath)
    {
        BufferedImage bi = null;
        try
        {
            bi = ImageIO.read(new File(imgpath));
        }
        catch (Exception e) { e.printStackTrace(); }
        return bi;
    }

    /**
     * Scales an image given a ratio. 0<1 for a smaller image ; >1 for a bigger image
     * @param bi the image
     * @param scaleValue the ratio
     * @return the scaled image
     */
    public static BufferedImage scaleImage(BufferedImage bi, double scaleValue)
    {
        AffineTransform tx = new AffineTransform();
        tx.scale(scaleValue, scaleValue);
        AffineTransformOp op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);
        BufferedImage biNew = new BufferedImage( (int) (bi.getWidth() * scaleValue),
                                                 (int) (bi.getHeight() * scaleValue),
                                                 BufferedImage.TYPE_INT_ARGB); // bi.getType() thrown an exception with PNG images
        return op.filter(bi, biNew);
    }


    /**
     * Draw image whatever happens.
     * @param g
     */
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        // Center thumb
        int th = this.getHeight();
        int tw = this.getWidth();
        int ih = this.image.getHeight();
        int iw = this.image.getWidth();

        this.x = Math.round( (tw - iw) / 2 ); // start X position
        this.y = Math.round( (th - ih) / 2 ); // start Y position

        // DEBUG
        System.out.println("THUMBNAIL CONTAINER SIZE : " + tw + " x " + th
                         + " | THUMBNAIL IMAGE SIZE : " + iw + " x " + ih
                         + " | THUMBNAIL POSITION : x = " + this.x + " y = " + this.y);

        // Display...
        g.drawImage(image, this.x, this.y, null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        setName("Form"); // NOI18N
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 398, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 298, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_formMouseClicked
    {//GEN-HEADEREND:event_formMouseClicked
        ImageFrame imgf = new ImageFrame(this.imagePath);
        // DEBUG
        System.out.println("DISPLAYING IMAGE AT : " +  this.imagePath);
    }//GEN-LAST:event_formMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
